class Node(object):
    """结点"""
    def __init__(self, item):
        self.item = item
        self.next = None


class SingleLinkedList(object):
    """单链表"""
    def __init__(self, node=None):
        self.__head = node

    def is_empty(self):
        """链表是否为空
        :return: 如果链表为空, 返回True
        """
        return self.__head is None

    def length(self):
        """链表长度
        :return: 返回count的累加计数
        """
        cur = self.__head
        count = 0
        while cur is not None:
            count += 1
            cur = cur.next
        return count

    def travel(self):
        """遍历整个链表"""
        cur = self.__head
        while cur is not None:
            print(cur.item, end=" ")
            cur = cur.next
        print()

    def add(self, item):
        """链表头部添加元素"""
        node = Node(item)
        node.next = self.__head
        self.__head = node

    def append(self, item):
        """链表尾部添加元素"""
        node = Node(item)
        # 如果链表为空, 单独处理
        if self.is_empty():
            self.__head = node
        else:
            cur = self.__head
            while cur.next is not None:
                cur = cur.next
            # 退出循环, 游标指向尾结点
            cur.next = node

    def insert(self, pos, item):
        """链表指定位置插入元素"""
        # 在头部插入
        if pos <= 0:
            self.add(item)
        # 在尾部插入
        elif pos >= self.length():
            self.append(item)
        # 在中间插入
        else:
            cur = self.__head
            count = 0
            while count < pos - 1:
                count += 1
                cur = cur.next
            # 退出循环时, 游标指向pos的前一个位置
            node = Node(item)
            node.next = cur.next
            cur.next = node

    def remove(self, item):
        """根据数据值删除结点"""
        cur = self.__head
        pre = None
        while cur is not None:
            if cur.item == item:
                # 要删除的是头结点
                if cur == self.__head:
                    self.__head = cur.next
                    cur.next = None
                else:
                    pre.next = cur.next
                    cur.next = None
                return
            # 游标pre跟踪cur移动, 且比cur落后一个位置
            pre = cur
            cur = cur.next

    def search(self, item):
        """根据数据值查找结点是否存在"""
        cur = self.__head
        while cur is not None:
            if cur.item == item:
                return True
            cur = cur.next
        return False


if __name__ == '__main__':
    ll = SingleLinkedList()
    print(ll.length())
    print("is empty? {}".format(ll.is_empty()))
    ll.travel()
    # 尾部添加
    ll.append(1)  # 1
    print(ll.length())
    ll.travel()
    # 尾部添加
    ll.append(2)  # 1, 2
    print(ll.length())
    ll.travel()
    # 头部添加
    ll.add(3)  # 3, 1, 2
    ll.travel()
    # 头部插入
    ll.insert(0, 4)  # 4, 3, 1, 2
    ll.travel()
    # 尾部插入
    ll.insert(20, 5)  # 4, 3, 1, 2, 5
    ll.travel()
    # 中间插入
    ll.insert(2, 6)  # 4, 3, 6, 1, 2, 5
    ll.travel()
    # 删除头
    ll.remove(4)  # 3, 6, 1, 2, 5
    ll.travel()
    # 删除尾
    ll.remove(5)  # 3, 6, 1, 2
    ll.travel()
    # 删除中间
    ll.remove(6)  # 3, 1, 2
    ll.travel()
    # 删除剩余
    ll.remove(3)  # 1, 2
    ll.travel()
    ll.remove(1)  # 2
    ll.travel()
    ll.remove(2)  # 空
    ll.travel()

